<!-- 游戏活动 -->
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="display: flex;justify-content: space-between;margin-bottom: -10px;">
			<div>
				<p class="text">游戏活动管理</p>
			</div>
			<el-form :inline="true">
				<el-form-item >
					<el-button type="primary" @click="adddialogVisible = true" icon="el-icon-plus">添加</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="GameList" highlight-current-row v-loading="loading" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
			<el-table-column type="index" align="center" label="ID" width="80"></el-table-column>
			<el-table-column prop="gameName" align="center" label="游戏名称"></el-table-column>
			<el-table-column prop="stillAmount" align="center" label="奖池柚子数" width="105"></el-table-column>
			<el-table-column prop="wasteAmount" align="center" label="消耗柚子数" width="105"></el-table-column>
			<el-table-column prop="putPersoncount" align="center" label="已投人数" width="105"></el-table-column>
			<el-table-column prop="putAmount" align="center" label="已投柚子数" width="105"></el-table-column>
			<el-table-column prop="bonusUserid" align="center" label="中奖/预中奖用户ID" width="140" style="display: none;"></el-table-column>
			<el-table-column align="center" label="状态" width="100">
				<template slot-scope="scope">
					<p v-if="scope.row.gameStatus == '0'">待开始</p>
					<p v-if="scope.row.gameStatus == '1'">已开始</p>
					<p v-if="scope.row.gameStatus == '2'">已结束</p>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" align="center" label="创建时间" width="160" ></el-table-column>
			<el-table-column prop="startTime" align="center" label="开始时间" width="160"></el-table-column>
			<el-table-column prop="endTime" align="center" label="结束时间" width="160" ></el-table-column>
			<el-table-column label="操作" width="300">
				<template slot-scope="scope">
					<el-button size="small" type="warning" @click="handleTool(scope.$index, scope.row)" icon="el-icon-s-tools">配置</el-button>
					<el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)" icon="el-icon-edit">修改</el-button>
					<el-button size="small" type="danger" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--批量删除-->
		<el-col :span="24" class="toolbar">
			<el-pagination background layout="total,sizes,prev, pager, next,jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!-- 添加活动 -->
		<el-dialog title="添加活动" :visible.sync="adddialogVisible" width="73%" :before-close="handleClose">
			<el-form :model="addGameForm" label-width="100px" :rules="addGameFormRules" ref="addGameForm">
				<el-form-item label="游戏名称" prop="gameName">
					<el-input v-model="addGameForm.gameName" auto-complete="off" placeholder="请输入游戏名称"></el-input>
				</el-form-item>
				<el-form-item label="开始时间" prop="startTime">
					<el-date-picker v-model="addGameForm.startTime" :picker-options="expireTimeOption" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择开始时间" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item label="预中奖人员ID" prop="bonusUserid">
					<el-input v-model="addGameForm.bonusUserid" auto-complete="off" placeholder="请输入预中奖人员"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="adddialogVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addGameSubmit" :loading="loading">提交</el-button>
			</div>
		</el-dialog>
		<!-- 配置规则列表 -->
		<el-dialog title="规则配置列表" :visible.sync="dialogVisible" width="73%" :before-close="handleClose">
			<el-table :data="ToolList" highlight-current-row v-loading="loading" border style="width: 100%;">
				<el-table-column type="index" align="center" label="ID" width="80"></el-table-column>
				<el-table-column prop="shaddockNum" align="center" label="起始柚子数"></el-table-column>
				<el-table-column prop="startNum" align="center" label="开始柚子数"></el-table-column>
				<el-table-column prop="endNum" align="center" label="结束柚子数"></el-table-column>
				<el-table-column prop="lateSecond" align="center" label="延迟秒数"></el-table-column>
				<el-table-column label="操作" width="150" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="danger" @click="handleToolDel(scope.$index, scope.row)" icon="el-icon-delete">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="addFormVisible = true ">添加</el-button>
			</span>
		</el-dialog>
		<!-- 添加游戏活动规则 -->
		<el-dialog title="添加游戏活动规则" :visible.sync="addFormVisible" width="50%" :before-close="handleClose">
			<el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
				<el-form-item label="起始柚子数" prop="shaddockNum">
					<el-input v-model="addForm.shaddockNum" auto-complete="off" placeholder="请输入起始柚子数"></el-input>
				</el-form-item>
				<el-form-item label="开始柚子数" prop="startNum">
					<el-input v-model="addForm.startNum" auto-complete="off" placeholder="请输入开始柚子数"></el-input>
				</el-form-item>
				<el-form-item label="结束柚子数" prop="endNum">
					<el-input v-model="addForm.endNum" auto-complete="off" placeholder="请输入结束柚子数"></el-input>
				</el-form-item>
				<el-form-item label="延迟秒数" prop="lateSecond">
					<el-input v-model="addForm.lateSecond" auto-complete="off" placeholder="延迟秒数"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addToolSubmit" :loading="loading">提交</el-button>
			</div>
		</el-dialog>
		<!-- 修改活动 -->
		<el-dialog title="修改游戏活动" :visible.sync="editFormVisible" width="50%" :before-close="handleClose">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="游戏名称" prop="gameName">
					<el-input v-model="editForm.gameName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="开始时间" prop="startTime">
					<el-date-picker v-model="editForm.startTime" type="datetime" :picker-options="expireTimeOption" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择开始时间" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item label="预中奖人员ID" prop="bonusUserid">
					<el-input v-model="editForm.province" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="结束时间" prop="endTime">
					<el-date-picker v-model="editForm.endTime" type="datetime" :picker-options="expireTimeOption" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择结束时间" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item label="状态" prop="gameStatus">
					<el-radio-group v-model="editForm.status">
						<el-radio class="radio" :label="0">待开始</el-radio>
						<el-radio class="radio" :label="1">开始</el-radio>
						<el-radio class="radio" :label="2">结束</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="loading">确定</el-button>
			</div>
		</el-dialog>
	</section>
</template>
<script>
	import util from '../../../common/js/util'
	import {
		getGameList,
		getGameRuleList,
		addGame,
		editGame,
		delGame,
		addGameRule,
		delGameRule,
		Refreshtoken
	} from '../../../api/api';
	
	export default {
		data() {
			return {
				filters: {
					text: ''
				},
				loading: false,
				select: '',
				vid:'',
				GameList: [],
				ToolList:[],
				sels: [],//列表选中列81560
				total: 0,
				page: 1,
				size:10,
				period:0,
				timer: null,
				dialogVisible:false,
				addFormVisible:false,
				addForm:{startNum:'',endNum:'',shaddockNum:'',lateSecond:''},
				addFormRules:{
					startNum: [{
						required: true,
						message: '起始柚子数',
						trigger: 'blur'
					}],
					endNum: [{
						required: true,
						message: '开始柚子数',
						trigger: 'blur'
					}],
					shaddockNum: [{
						required: true,
						message: '结束柚子数',
						trigger: 'blur'
					}],
					lateSecond: [{
						required: true,
						message: '延迟秒数',
						trigger: 'blur'
					}],
				},
				editFormVisible:false,//弹出层
				// 修改界面数据
				editForm: {
					gameName: '',
				},
				editFormRules: {
					gameName: [{
						required: true,
						message: '请输入游戏名称',
						trigger: 'blur'
					}],
				},
				goodsID:'',
				adddialogVisible:false,
				addGameForm:{gameName:'',startTime:'',bonusUserid:0},
				addGameFormRules:{
					gameName: [{
						required: true,
						message: '请输入游戏名称',
						trigger: 'blur'
					}],
				},
				expireTimeOption: {
					disabledDate(date) {
					return date.getTime() <= Date.now();
					}
				},
			}
		},
		mounted() {
			this.getGame()
			this.queryInfo();
			this.timer = setInterval(() => {
			   setTimeout(this.queryInfo, 0)
		    }, 600000)
		},
		methods: {
			// 设置定时器
			queryInfo() {
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				let para = {token: Token,timestamp: timestamp};
				Refreshtoken(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.loading = false;
					}
				});
			},
			//获取游戏活动列表
			getGame:function(){
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				var para = {token: Token,curpage:this.page,pagesize:this.size,timestamp: timestamp};				
				this.loading = true;
				getGameList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.total = res.data.total;
						this.GameList = res.data.list;
						this.loading = false;
					}
				});
			},
			// 显示规则配置
			handleTool(index,row){
				this.goodsID = row.id
				this.dialogVisible = true
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				var para = {token: Token,gameId:row.id,timestamp: timestamp};				
				this.loading = true;
				getGameRuleList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.ToolList = res.data;
						this.loading = false;
					}
				});
			},
			
			//添加游戏活动规则
			handleTooladd(index, row){
				this.addFormVisible = true
			},
			addToolSubmit(){
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						var Token = sessionStorage.getItem('Token');
						var timestamp = new Date().getTime();
						var para = {token: Token,gameId:this.goodsID,shaddockNum:this.addForm.shaddockNum,
						startNum:this.addForm.startNum,endNum:this.addForm.endNum,lateSecond:this.addForm.lateSecond,timestamp: timestamp};
						this.$confirm('确认提交游戏活动规则吗？', '提示', {
							type: 'warning'
						}).then(() => {
							this.loading = true;
							addGameRule(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.loading = false;
									this.Tool(this.goodsID)
								}
							});
						})
					}
				});
			},
			Tool(ID){
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				var para = {token: Token,gameId:ID,timestamp: timestamp};				
				this.loading = true;
				getGameRuleList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.ToolList = res.data;
						this.loading = false;
					}
				});
			},
			//删除游戏活动规则
			handleToolDel(index, row){
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				var para = {token: Token,ids:row.id,timestamp: timestamp};
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.loading = true;
					delGameRule(para).then((res) => {
						var res = res.data;
						if (res.code != 0) {
							this.$message({
								message: res.message,
								type: 'error'
							});
						} else {
							this.loading = false;
							this.Tool(this.goodsID)
						}
					});
				}).catch(() => {
							
				});
			},
			// 添加活动
			addGameSubmit(){
				this.$refs.addGameForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认添加该活动吗？', '提示', {}).then(() => {
							this.loading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							let para = Object.assign({token: Token, timestamp:timestamp,}, this.addGameForm);
							addGame(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.loading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['addGameForm'].resetFields();
									this.adddialogVisible = false;
									this.getGame();
								}
							});
						});
					}
				});
			},
			// 修改角色
			handleEdit(index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			handleClose(done) {
				this.$confirm('确认关闭？')
				    .then(_ => {
						done();
					})
					.catch(_ => {});
			},
			//修改活动
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交修改的信息吗？', '提示', {}).then(() => {
							this.loading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							let para = Object.assign({token: Token, timestamp:timestamp,}, this.editForm);
							editGame(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.loading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getGame();
								}
							});
						});
					}
				});
			},
			// 通过审核
			handleReview:function(index, row){
				this.$confirm('是否通过该用户城市合伙人的申请?', '提示', {
				  confirmButtonText: '确定',
				  cancelButtonText: '取消',
				}).then(() => {
				  this.$message({
					type: 'success',
					message: '审核通过!'
				  });
				  this.Review(row.userId,1)
				}).catch(() => {
				  this.$message({
					type: 'info',
					message: '已取消'
				  });
				});
			},
			//删除
			handleDel:function(index,row){
				this.$confirm('确认删除该条游戏活动吗？', '提示', {}).then(() => {
					this.loading = true;
					var Token = sessionStorage.getItem('Token');
					var timestamp = new Date().getTime();
					let para = {token: Token, timestamp:timestamp,ids:row.id};
					delGame(para).then((res) => {
						this.loading = false;
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getGame()
					});
				});
			},
			Review(userId,status){
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				let para = {token: Token,timestamp: timestamp,userId:userId,status:status};
				ReviewPartner(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.loading = false;
						this.getUsersList();
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			cellStyle({row, column}){
				return 'text-align:center;padding:2px;';
			},
			rowClass({row, column}){
				return 'text-align:center';
			},
			handleSizeChange(val) {
				if(this.filters.text == ''){
					this.size = val;
					this.getUsersList();
				}else{
					this.size = val;
					this.getUsersList(this.filters.text);
				}
			},
			handleCurrentChange(currentPage) {
				this.page = currentPage;
				this.getUsersList();
			},
			// 获取用户详情
			handleDetail:function(index, row){
				this.$router.push({
					path:'/userDetail',
					query: {
						mallCode: row.id
					}
				});
			}
		},
		
		beforeDestroy() {
		  clearInterval(this.timer);        
		  this.timer = null;
		}
	};
</script>

<style scoped>
.el-button.is-disabled{
	color: #bfcbd9;
	    cursor: not-allowed;
	    background-image: none;
	    background-color: #eef1f6;
	    border-color: #d1dbe5;
}
.el-pagination {
    white-space: nowrap;
    padding: 2px 5px;
    color: #48576a;
}
.el-select .el-input .el-input--suffix .el-input__inner {
    width: 130px !important;
  }  
</style>
